import unittest
import time

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.maximize_window()
        self.driver.get('https://www.baidu.com/')

    def tearDown(self):
        time.sleep(10)
        self.driver.quit()

    def test_search(self):
        search = self.driver.find_element(By.ID, 'kw')
        search.send_keys("web自动化测试")
        search.send_keys(Keys.ENTER)
        time.sleep(3)
        # 鼠标右键
        # ActionChains(self.driver).context_click(search).perform()
        # time.sleep(2)

        # 鼠标双击
        ActionChains(self.driver).click(search).double_click(search).perform()
        time.sleep(10)
        # 键盘复制
        # search.send_keys(Keys.CONTROL, 'a')
        # time.sleep(2)
        search.send_keys(Keys.CONTROL, 'c')
        time.sleep(2)
        # 设置 鼠标悬停
        set = self.driver.find_element(By.NAME, 'tj_settingicon')
        ActionChains(self.driver).move_to_element(set).perform()
        time.sleep(10)
        # 高级搜索
        self.driver.find_element(By.LINK_TEXT, '高级搜索').click()
        time.sleep(2)
        # 输入框1 q1
        q1 = self.driver.find_element(By.NAME, 'q1')
        q1.send_keys(Keys.CONTROL, 'v')
        time.sleep(3)
        # 双击输入框1的内容 复制  剪切到输入框4中
        ActionChains(self.driver).double_click(q1).perform()
        time.sleep(2)
        q1.send_keys(Keys.CONTROL, 'c')
        time.sleep(2)
        q1.send_keys(Keys.CONTROL, 'x')
        time.sleep(2)
        # 粘贴
        self.driver.find_element(By.NAME, 'q4').send_keys(Keys.CONTROL, 'v')
        time.sleep(5)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-gpc"]/div/div[1]/i[1]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-gpc"]/div/div[2]/div[2]/p[5]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-ft"]/div/div[1]/i[1]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-ft"]/div/div[2]/div[2]/p[3]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-8"]/input[2]').click()
        time.sleep(5)

        # 获取浏览器名称（如 "chrome", "firefox", "edge"）
        browser_name = self.driver.capabilities['browserName']
        print(f"当前浏览器: {browser_name}")
        # self.assertEqual(browser_name, "firefox")  # 检查是否为 firefox

    def test_search_01(self):
        search = self.driver.find_element(By.ID, 'kw')
        search.send_keys("web自动化测试2")
        search.send_keys(Keys.ENTER)
        time.sleep(3)
        # 鼠标双击
        ActionChains(self.driver).click(search).double_click(search).perform()
        time.sleep(10)
        search.send_keys(Keys.CONTROL, 'c')
        time.sleep(2)
        # 设置 鼠标悬停
        set = self.driver.find_element(By.CLASS_NAME, 'pf')
        ActionChains(self.driver).move_to_element(set).perform()
        time.sleep(10)
        # 高级搜索
        self.driver.find_element(By.LINK_TEXT, '高级搜索').click()
        time.sleep(2)
        # 输入框1 q1
        q1 = self.driver.find_element(By.NAME, 'q1')
        q1.send_keys(Keys.CONTROL, 'v')
        time.sleep(3)
        # 双击输入框1的内容 复制  剪切到输入框4中
        ActionChains(self.driver).double_click(q1).perform()
        time.sleep(2)
        # 粘贴
        self.driver.find_element(By.NAME, 'q4').send_keys(Keys.CONTROL, 'v')
        time.sleep(5)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-gpc"]/div/div[1]/i[1]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-gpc"]/div/div[2]/div[2]/p[5]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-ft"]/div/div[1]/i[1]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-ft"]/div/div[2]/div[2]/p[3]').click()
        time.sleep(2)
        self.driver.find_element(By.XPATH, '//*[@id="adv-setting-8"]/input[2]').click()
        time.sleep(5)

if __name__ == '__main__':
    unittest.main()
